$(document).ready(function() {
    $("#editable").dataTable();
});
//添加角色
function AddRole(){
    var RoleName = $("#AddRole .RoleName").val();
    var RoleCode = $("#AddRole .RoleCode").val();
    var RoleRemark = $("#AddRole .RoleRemark").val();
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/createrole',
        data: {RoleName:RoleName,RoleCode:RoleCode,RoleRemark:RoleRemark},
        success: function (ret) {
            if(ret.IsBizSuccess){
                var str = '<button class="btn btn-success btn-info  btn-xs editForm" type="button" data-name="'+RoleName+'" data-code="'+RoleCode+'" data-remark="'+RoleRemark+'" data-roleid="'+ret.RoleId+'"><i class="fa fa-pencil-square-o"></i> 编辑</button>\
    <button class="btn btn-success btn-default btn-xs ModalInfoList" type="button"  data-roleid="'+ret.RoleId+'"><i class="fa fa-cog"></i> 设置</button>';

                $('#AddRole').modal('hide');
                layer.msg('添加成功！');
                $("#editable").dataTable().fnAddData([RoleName,RoleCode,RoleRemark,str]);
                $("#AddRole .RoleName").val("");
                $("#AddRole .RoleCode").val("");
                $("#AddRole .RoleRemark").val("");
            }else{
                $('#EditRole').modal('hide');
                layer.msg('操作失败！');
            }
        }
    });
}
//编辑角色
var _this = '';
$(document).on('click','.editForm',function(){
    var RoleName = $(this).data('name');
    var RoleCode = $(this).data('code');
    var RoleRemark = $(this).data('remark');
    $("#UpdateRole").attr("data-roleid",$(this).data('roleid'));
    _this = $(this);
    $("#EditRole .RoleName").val(RoleName);
    $("#EditRole .RoleCode").val(RoleCode);
    $("#EditRole .RoleRemark").val(RoleRemark);
    $('#EditRole').modal('show');
});
//修改
function UpdateRole(){
    var RoleName = $("#EditRole .RoleName").val();
    var RoleCode = $("#EditRole .RoleCode").val();
    var RoleRemark = $("#EditRole .RoleRemark").val();
    var RoleId = $("#UpdateRole").attr("data-roleid");;
    var _that = _this;
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/updaterole',
        data: {RoleName:RoleName,RoleCode:RoleCode,RoleRemark:RoleRemark,RoleId:RoleId},
        success: function (ret) {
            if(ret.IsBizSuccess){
                $('#EditRole').modal('hide');
                _that.data("code",RoleCode).data("name",RoleName).data("remark",RoleRemark).parent().prev().html(RoleRemark).prev().html(RoleCode).prev().html(RoleName);
                layer.msg('编辑成功！');

            }else{
                $('#EditRole').modal('hide');
                layer.msg('操作失败！');

            }
        }
    });
}


//获取 模块信息列表
var RoleList = new Array();
$(document).on('click','.ModalInfoList',function(){
    var RoleId = $(this).data('roleid');
    $(this).parent().parent().addClass("success").siblings().removeClass("success");

    if( (typeof(RoleList[RoleId]) != "undefined")){
        $('#jstree'+RoleId).addClass("show").siblings().removeClass("show");
        return false;
    }
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/get-role-action-list',
        data: {RoleId:RoleId},
        success: function (ret) {
            var str = ' <div id="jstree'+RoleId+'" data-roleid="'+RoleId+'" style="display:none"><ul>';
            $.each(ret,function(key,val){
                    if((typeof(val.childs) != "undefined") && (val.childs.length > 0)){
                        var successlength = 0;
                        var childsStr = '';
                    $.each(val.childs,function(ke,va){
                        if(va.isselected  == true){
                            successlength ++ ;
                            childsStr += '<li data-jstree=\'{"type":"input","selected":"true"}\' data-code="'+va.ControllerCode+'">'+va.Name+'</li>';
                        }else{
                            childsStr += '<li data-jstree=\'{"type":"input"}\' data-code="'+va.ControllerCode+'">'+va.Name+'</li>';
                        }
                    });
                        if(val.childs.length == successlength){
                            str += '<li class="jstree-open" data-jstree=\'{"selected":"true"}\' >'+val.Name+"<ul>"+childsStr;
                        }else{
                            str += '<li class="jstree-open" >'+val.Name+"<ul>"+childsStr;
                        }

                    str += '</ul>';
                }else{
                    //str += '<li class="jstree-open customtree" data-jstree=\'{"type":"input"}\'>'+val.Name;
                }
                str+='</li>';
            });
            str += '</ul></div>';
            RoleList[RoleId] =  RoleId;
            $("#RoleMenuList").append(str);
            $('#jstree'+RoleId).addClass("show").siblings().removeClass("show");
            jsTree( $('#jstree'+RoleId));
            $("#RoleUpdate").show();
        }
    });
});
//模块信息列表 生成树
function jsTree(obj){
    obj.jstree({
        "core" : {
            "multiple" : true, // 允许多选
            'animation' : false
        },
        'expand_selected_onload' : false, //选中项蓝色底显示
        //"plugins": ["types", ],
        'plugins' : ["types","dnd",'checkbox', 'search'],  //如果使用checkbox效率会降低, 'wholerow'会把线隐藏掉
        "types": {
            "default": {
                "icon": "fa fa-folder"
            },
            "input": {
                "icon": "fa fa-square-o"
            }
        }
    }).bind('click.jstree', function(event) {
    });

}

//保存角色列表
$(document).on('click','#RoleUpdate',function(){
    var obj = $("#RoleMenuList").find(".show");
    if(obj.length > 0){
        var RoleId = obj.data('roleid');
        var Codes = new Array();
        obj.find('.jstree-children').each(function(k,v){
            $(this).find(".jstree-children li").each(function(key,val){
                if($(this).attr('aria-selected') == 'true'){
                    Codes.push($(this).data('code')+'-Index')
                }
            });
        });
        var JsonData = {RoleId:RoleId,Codes:Codes};
        $.ajax({
            type: "post",
            dataType: "json",
            url: '/role/role-action-update',
            data: JsonData,
            success: function (ret) {
                if(ret.IsBizSuccess){
                    layer.open({
                        title: '提示'
                        ,content: '操作成功！'
                    });
                }else{
                    layer.open({
                        title: '提示'
                        ,content: '操作失败！'
                    });
                }
            }
        });
    }else{
        layer.open({
            title: '提示'
            ,content: '请选择角色！'
        });
    }

});

